package com.fyzk.activity.lottery.shuzicai;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.down_numhistory.NumHistoryPrizeView;
import com.fyzk.activity.lottery.basketball.JCLQSelectPlayMethodPop;
import com.fyzk.activity.myview.BallPopFrameLayout;
import com.fyzk.activity.myview.BallScrollView;
import com.fyzk.activity.myview.DrawBallFrameLayout;
import com.fyzk.activity.myview.WeiZhi;
import com.fyzk.activity.trend.SSQTrendActivity;
import com.fyzk.bean.PrizeHistoryItem;
import com.fyzk.betzhushu.ZhuShuFactory;
import com.tyjc.lottery.R;
import com.fyzk.random.RandomFactory;
import com.fyzk.utils.HelpUrlConstants;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.TopTitleView;
import com.fyzk.view.ViewCallBack;

import java.util.ArrayList;
import java.util.List;

import static com.tyjc.lottery.R.id.shake_random_ll;


public class SSQActivity extends LotteryBaseActivity implements
        JCLQSelectPlayMethodPop
        .OnWFCheckChangedListener {

    private BallPopFrameLayout moveQiu;

    private TopTitleView topView;

    private DrawBallFrameLayout ball_linear;
    private BallScrollView ball_scroll;

    private PopSSQSelectPlayMethod popSSQSelectPlayMethod;

    SelectBallView ballView_firstLine;
    SelectBallView ballView_twoline;
    SelectBallView ballView_threeline;

    List<BallInfo> balls_firstLine = new ArrayList<BallInfo>();
    List<BallInfo> balls_twoLine = new ArrayList<BallInfo>();
    List<BallInfo> balls_threeLine = new ArrayList<BallInfo>();

    private TextView bottom_zhushu;

    private TextView bottom_money;

    private LinearLayout ll_history;

    private final String[] pop_titles = {"直选", "胆拖", "介绍"};

    private LinearLayout dan_ll;
    private TextView ballTitle_hong;
    private TextView ballTitle_lan;
    private TextView hong_tishi;
    private LinearLayout yao_ll;

    private int wanFaId = LotteryIDUtils.SSQ_LOTTERY_COMMON_DAN_ID_INT;

    protected int hong_count = 33;
    protected int lan_count = 16;
    protected int dan_max = 5;
    private final int MAX_HONG = 16;
    private final int MAX_TUOMA = 16;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_ssq);
        initBallView();
        ViewCallBack.getInstance().AttachCountDownObj(this);
    }

    @Override
    public TopTitleView getTopView() {
        return topView;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    }

    private void initBallView() {

        TouZhuHelper.getInstance().setBallClickListener(this);
        TouZhuHelper.getInstance().setBallItemClickLitener(this);

        topView = (TopTitleView) findViewById(R.id.topTitle);
        topView.setRightVisible(View.VISIBLE);
        topView.setJianTouVisible(View.VISIBLE);
        topView.setCenterText(R.string.ssq);
        topView.setOnTitleClickListener(this);

        popSSQSelectPlayMethod = new PopSSQSelectPlayMethod(this,
                AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
        popSSQSelectPlayMethod.setOnWFCheckChangedListener(this);
        moveQiu = (BallPopFrameLayout) findViewById(R.id.pl5_fl_moveqiu);
        ball_linear = (DrawBallFrameLayout) findViewById(R.id.pl5_drawball_framelayout);
        ball_scroll = (BallScrollView) findViewById(R.id.pl5_ballscroll);
        ball_linear.setScr(ball_scroll);
        ball_linear.setFl_moveqiu(moveQiu);
        ball_linear.setUpOrDownListener(this);
        ballView_firstLine = (SelectBallView) findViewById(R.id.firstLine);
        ballView_twoline = (SelectBallView) findViewById(R.id.twoLine);
        ballView_threeline = (SelectBallView) findViewById(R.id.thressLine);
        bottom_zhushu = (TextView) findViewById(R.id.pl5_bottom_tv_zhushu);
        bottom_money = (TextView) findViewById(R.id.pl5_bottom_tv_money);

        dan_ll = (LinearLayout) findViewById(R.id.ssq_dan_ll);
        ballTitle_hong = (TextView) findViewById(R.id.ssq_balltitle_h);
        ballTitle_lan = (TextView) findViewById(R.id.ssq_balltitle_l);
        hong_tishi = (TextView) findViewById(R.id.ssq_hong_tishi);
        yao_ll = (LinearLayout) findViewById(shake_random_ll);
        yao_ll.setOnClickListener(this);
        //新加
        ll_history = (LinearLayout) findViewById(R.id.ssq2_ball_history);

        initArrayInfos();

        ballView_firstLine.setSelect_max(dan_max);//胆拖最多可选5个球
        ballView_twoline.setSelect_max(MAX_HONG);

        //设置关联选球SelectView
        ballView_firstLine.setGuanLian_view(ballView_twoline);
        ballView_twoline.setGuanLian_view(ballView_firstLine);


        initBottomView();
    }

    @Override
    protected void initArrayInfos() {
        if (arr_infos.size() == 3) {
            balls_firstLine = arr_infos.get(0);
            balls_twoLine = arr_infos.get(1);
            balls_threeLine = arr_infos.get(2);
        } else {
            arr_infos.clear();
            initBallList(balls_firstLine, hong_count, 1,true);
            initBallList(balls_twoLine, hong_count, 1,true);
            initBallList(balls_threeLine, lan_count, 1,true);
            arr_infos.add(balls_firstLine);
            arr_infos.add(balls_twoLine);
            arr_infos.add(balls_threeLine);
        }

        ballView_firstLine.setBallInfoList(balls_firstLine);
        ballView_twoline.setBallInfoList(balls_twoLine);
        ballView_threeline.setBallInfoList(balls_threeLine);
    }

    @Override
    public void onCenterClick() {
        super.onCenterClick();
        popSSQSelectPlayMethod.showAsDropDown(topView);

    }

    @Override
    public void ballClick(WeiZhi info) {
        // TODO Auto-generated method stub
        WeiZhi w = info;
        // TODO Auto-generated method stub
        int left = w.getLeft() - 5;
        int right = w.getRight() + 5;
        int top = (int) ((ball_linear.getY1() - w.getPointY() + w.getTop())
                - (w.getBottom() - w.getTop()) - 10);
        int bottom = (int) (ball_linear.getY1() - w.getPointY() + w.getTop());
        Rect rect = new Rect(left, top, right, bottom);
        moveQiu.setRect(rect, w.getNum(), w.getType(), w.isShowMoveBall);
    }

    @Override
    public void ballClick() {
        long zhushu = getZhushu();
        long money = zhushu * 2;
        bottom_zhushu.setText(zhushu + "注 ");
        bottom_money.setText("共" + money + "米");
    }

    private long getZhushu() {
        return ZhuShuFactory.getZhuShu(wanFaId).betZhuShu(arr_infos);
    }

    @Override
    public boolean touZhu() {
        long zhuShu = getZhushu();
        if (zhuShu > 0) {
            int lotteryId = wanFaId;
            if (wanFaId != LotteryIDUtils.SSQ_LOTTERY_TOWED_ID_INT) {
                lotteryId = zhuShu > 1 ? LotteryIDUtils.SSQ_LOTTERY_COMMON_FU_ID_INT
                        : LotteryIDUtils.SSQ_LOTTERY_COMMON_DAN_ID_INT;
            }

            touZhu1(lotteryId, zhuShu);

            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onRightClick() {
        toHelp();
    }

    @Override
    public int getLotteryId() {
        return LotteryIDUtils.SSQ_LOTTERY_ID_INT;
    }

    @Override
    public void randomBallNum() {
        int two[] = RandomFactory.getRandom(getLotteryId()).randomDanHang(6);
        int thire = RandomFactory.getRandom(getLotteryId()).randomLan(1)[0];

        initBallList(balls_twoLine, hong_count, 1,true);
        initBallList(balls_threeLine, lan_count, 1,true);

        for (int i = 0; i < two.length; i++) {
            balls_twoLine.get(two[i]).setSelected(true);
        }
        balls_threeLine.get(thire).setSelected(true);

        ballView_twoline.invalidate();
        ballView_threeline.invalidate();

        ballClick();
    }

    @Override
    protected void addHistoryView(ArrayList<PrizeHistoryItem> historyItems, boolean isWaitting) {
        ll_history.removeAllViews();
        ll_history.addView(new NumHistoryPrizeView(getContext(), historyItems).getView());
    }


    @Override
    protected void popupClickItem(int arg2) {
        popupId = arg2;
        popSSQSelectPlayMethod.setCheckItem(arg2);
        switch (popupId) {
            case 0:
                onWFCheckChange(R.id.cb_selectplaymethod_normal_ssq, false);
                break;
            case 1:
                onWFCheckChange(R.id.cb_selectplaymethod_array_ssq, false);
                break;
        }

    }

    @Override
    public void clear() {
        bottom_zhushu.setText(0 + "注 ");
        bottom_money.setText("共" + 0 + "米");

        initBallList(balls_firstLine, hong_count, 1,true);
        initBallList(balls_twoLine, hong_count, 1,true);
        initBallList(balls_threeLine, lan_count, 1,true);

        ballView_firstLine.setSelect_count(0);
        ballView_twoline.setSelect_count(0);
        ballView_threeline.setSelect_count(0);

        ballView_firstLine.invalidate();
        ballView_twoline.invalidate();
        ballView_threeline.invalidate();
    }

    @Override
    public String getHelpUrl() {
        return HelpUrlConstants.Help_SSQ;
    }

    @Override
    public String getHelpTitle() {
        return getString(R.string.help_ssq);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case shake_random_ll:
                randomBallNum();
                break;
        }
    }

    @Override
    public void onWFCheckChange(int id, boolean isChecked) {
        clear();
        switch (id) {
            case R.id.cb_selectplaymethod_normal_ssq:
                topView.setCenterText("双色球直选");
                yao_ll.setVisibility(View.VISIBLE);
                isYao = true;
                dan_ll.setVisibility(View.GONE);
                ballTitle_hong.setText(getString(R.string.ssq_balltitle_h_t1));
                ballTitle_lan.setText(getString(R.string.ssq_balltitle_l_t1));
                hong_tishi.setText(getString(R.string.ssq_hong_tishi1));
                wanFaId = LotteryIDUtils.SSQ_LOTTERY_ID_INT;
                ballView_twoline.setSelect_max(MAX_HONG);
                popupId = 0;
                break;
            case R.id.cb_selectplaymethod_array_ssq:
                yao_ll.setVisibility(View.INVISIBLE);
                isYao = false;
                topView.setCenterText(R.string.ssq_dantuo);
                dan_ll.setVisibility(View.VISIBLE);
                ballTitle_hong.setText(getString(R.string.ssq_balltitle_h_t2));
                ballTitle_lan.setText(getString(R.string.ssq_balltitle_l_t1));
                hong_tishi.setText(getString(R.string.ssq_hong_tishi2));
                wanFaId = LotteryIDUtils.SSQ_LOTTERY_TOWED_ID_INT;
                ballView_twoline.setSelect_max(MAX_TUOMA);
                popupId = 1;
                break;
        }
        popSSQSelectPlayMethod.dismiss();

    }

    @Override
    protected void addMissValue() {
        super.addMissValue();
        if (missInfo != null) {
            ballView_firstLine.setMissValues(missInfo.missValue("1001MV_X").get(0));
            ballView_twoline.setMissValues(missInfo.missValue("1001MV_X").get(0));
            ballView_threeline.setMissValues(missInfo.missValue("1001MV_X").get(1));

        }

    }

    @Override
    public Intent getTrendIntent() {
        Intent intent = new Intent(getContext(), SSQTrendActivity.class);
        intent.putExtra(IntentParamUtils.LOTTERYID, getLotteryId());
        return intent;
    }
}
